package com.huatai.exchange;

import com.faneui.common.bean.AbstractLogger;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.integration.base.service.CommunicationService;
import com.huatai.exchange.integration.base.service.DictService;
import com.huatai.exchange.integration.base.service.WorkDayService;
import com.huatai.exchange.utils.GzFundUtils;
import com.huatai.exchange.utils.O32QueryUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Map;

public abstract class AbstractService extends AbstractLogger {


    @Autowired
    protected CommunicationService communicationService;

    @Autowired
    protected DictService dictService;

    @Autowired
    protected WorkDayService workDayService;

    @Autowired
    protected GzFundUtils gzFundInfoUtils;

    @Autowired
    protected O32QueryUtils o32QueryUtils;

    @Value("${tmp_file_path}")
    protected String tmpFilePath;


    protected String formatStr(String str, Map<String, Object> resvMap) throws BusinessException {
        if (StringUtils.isBlank(str)) {
            return str;
        }
        String time = System.currentTimeMillis() + "";
        logger.debug("formatStr time:{} start：{}", time, str);
        String tmpStr = str;
        String ztbh = (String) resvMap.get(Contents.FIELD_ZTBH);
        String ztbhs = (String) resvMap.get(Contents.FIELD_ZTBHS);
        String fundCode = (String) resvMap.get(Contents.FIELD_FUNDCODE);
        String yyyyMMdd = (String) resvMap.get(Contents.FIELD_YYYYMMDD);
        while (tmpStr.contains("${") && tmpStr.contains("}")) {
            Integer startNum = tmpStr.indexOf("${") + 2;
            Integer endNum = tmpStr.indexOf("}");
            String field = tmpStr.substring(startNum, endNum);
            logger.info("field:" + field);
            String value = (String) resvMap.get(field);
            logger.info("field_value:" + value);
            if (StringUtils.isBlank(value)) {
                switch (field) {
                    case Contents.FIELD_ZTMC:
                        value = gzFundInfoUtils.getFundName(ztbh);
                        break;
                    case Contents.FIELD_FUNDNAME:
                        value = o32QueryUtils.getFundName(fundCode);
                        break;
                    case Contents.FIELD_YYMMDD:
                        value = yyyyMMdd.substring(2);
                        break;
                    case Contents.FIELD_YYYY:
                        value = yyyyMMdd.substring(0, 4);
                        break;
                    case Contents.FIELD_YYYYMM:
                        value = yyyyMMdd.substring(0, 6);
                        break;
                    case Contents.FIELD_YYMM:
                        value = yyyyMMdd.substring(2, 6);
                        break;
                    case Contents.FIELD_YYYY_MM_DD:
                        value = DateFormatUtils.transfer(yyyyMMdd);
                        break;
                    case Contents.FIELD_MM_DD:
                        value = yyyyMMdd.substring(4, 6) + "-" + yyyyMMdd.substring(6, 8);
                        break;
                    case Contents.FIELD_MCDD:
                        value = transferMonth(yyyyMMdd.substring(4, 6)) + yyyyMMdd.substring(6, 8);
                        break;
                    case Contents.FIELD_MMDD:
                        value = yyyyMMdd.substring(4, 8);
                        break;
                    case Contents.FIELD_FUNDCODESSTR:
                        String fundCodes = (String) resvMap.get(Contents.FIELD_FUNDCODESSTR);
                        value = "'" + (fundCodes.replaceAll(",", "','")) + "'";
                        break;
                    case Contents.FIELD_NEXTWORK_YYYYMMDD:
                        value = workDayService.getNextWorkDay(yyyyMMdd);
                        break;
                    case Contents.FIELD_NEXTWORK_YYYY_MM_DD:
                        value = DateFormatUtils.transfer(workDayService.getNextWorkDay(yyyyMMdd));
                        break;
                    default:
                        value = "";
                        break;
                }
                if (StringUtils.isBlank(value)) {
                    throw new BusinessException("未识别标志[" + field + "]");
                }
                resvMap.put(field, value);
            }
            String oldStr = "${" + field + "}";

            if (str.contains("http") || str.contains("HTTP")) {
                //如果是HTTP请求,逗号转换
                String symbol = value.replaceAll(",","%2C");
                tmpStr = tmpStr.replace(oldStr, symbol);
            } else {
                tmpStr = tmpStr.replace(oldStr, value);
            }
        }
        logger.debug("formatStr  time:{}  end：{}", time, tmpStr);
        return tmpStr;
    }


    private String transferMonth(String mm) {
        String mc = mm;
        if ("10".equals(mm)) {
            mc = "a";
        } else if ("11".equals(mc)) {
            mc = "b";
        } else if ("12".equals(mc)) {
            mc = "c";
        } else {
            mc = mc.substring(1);
        }
        return mc;
    }
}
